package com.kehutong.common;

import org.coraframework.http.HttpClient;
import org.coraframework.inject.Inject;
import org.coraframework.inject.Singleton;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.webservice.WebServiceClient;
import org.coraframework.orm.exception.ORMException;
import org.coraframework.orm.jdbc.connection.DataBaseConfig;
import org.coraframework.orm.jdbc.connection.IDBConfig;
import org.coraframework.orm.jdbc.connection.ThreadLocalCache;
import org.coraframework.util.CallBack;

import java.io.IOException;

/**
 * 	服务器通讯客户端
 * @author liuzhen
 */
@Singleton
public class DoveClient {

	private static Logger logger = LoggerFactory.getLogger(DoveClient.class);

	private DoveClient() {
	}

	@Inject
	private WebServiceClient webserviceClient;

	public JSONObject post(String page, CallBack<HttpClient> httpCall) throws Exception {
		try {
			return webserviceClient.postJson(page, getCorpId(), httpCall);
		} catch (IOException e) {
			JSONObject json = new JSONObject();
			json.put("code", 0);
			json.put("message", "http状态错误");
			return json;
		}
//		HttpClient httpClient = WebServiceClient.create(getUrl(page) + page);
//		addCorpHeader(httpClient);
//		httpCall.call(httpClient);
//
//		String text = httpClient.post();
//		if (httpClient.getResponseCode() != 200) {
//			JSONObject json = new JSONObject();
//			json.put("code", httpClient.getResponseCode());
//			json.put("message", "http状态错误");
//			return json;
//		}

//		return JSON.parseObject(text);
	}

	public JSONObject postExternal(String url, CallBack<HttpClient> httpCall) throws Exception {
		try {
			return webserviceClient.postJson(url, getCorpId(), httpCall);
		} catch (IOException e) {
			JSONObject json = new JSONObject();
			json.put("code", 0);// TODO 具体编码
			json.put("message", "http状态错误");
			return json;
		}
//		HttpClient httpClient = WebServiceClient.create(url);
//		addCorpHeader(httpClient);
//		httpCall.call(httpClient);
//
//		String text = httpClient.post();
//		if (httpClient.getResponseCode() != 200) {
//			JSONObject json = new JSONObject();
//			json.put("code", httpClient.getResponseCode());
//			json.put("message", "http状态错误");
//			return json;
//		}
//
//		return JSON.parseObject(text);
	}

	public String postString(String page, CallBack<HttpClient> httpCall) throws Exception {
		try {
			return webserviceClient.postText(page, getCorpId(), httpCall);
		} catch (IOException e) {
			JSONObject json = new JSONObject();
			json.put("code", 0);
			json.put("message", "http状态错误");
			return e.getMessage();
		}
//		HttpClient httpClient = WebServiceClient.create(getUrl(page) + page);
//		addCorpHeader(httpClient);
//		httpCall.call(httpClient);
//
//		return httpClient.post();
	}

	public void post(String page, CallBack<HttpClient> httpCall, CallBack<JSONObject> dataCall) throws Exception {
		JSONObject rs = webserviceClient.postJson(page, getCorpId(), httpCall);
//		final HttpClient httpClient = HttpClient.create(getUrl(page) + page);
//		addCorpHeader(httpClient);
//		httpCall.call(httpClient);
//
//		String text = httpClient.get();
//		final JSONObject rs = JSON.parseObject(text);

		if (rs.getInteger("code") == 200) {
			dataCall.call(rs.getJSONObject("data"));
		} else {
			logger.error("{}", rs.getString("message"));
		}
	}

	public IDBConfig getDBConfig(String companyNo) throws Exception {
		JSONObject rs = post("/admin/service/database/get", http->{
			http.addParam("companyNo", companyNo);
		});

		if (rs.getInteger("code") == 200) {
			return new DataBaseConfig(rs.getJSONObject("data"));
		} else {
			throw new ORMException("unkown database for companyNo=" + companyNo);
		}
	}

	private String getCorpId() {
		return ThreadLocalCache.current().getCorpId();
	}
}
